package mq

import (
	"context"
	"fmt"

	"bmember/internal/service"

	"github.com/nsqio/go-nsq"
	"go.uber.org/zap"

	pay_pb "bmember/api/pb/client/pay/api/pay"
	pb "bmember/api/pb/server"
	"bmember/pkg/mq"
)

type mqHandler struct {
	*MqBase
	name string
}

// NewMqHandler new mq handler
func NewMqHandler() *mqHandler {
	return &mqHandler{
		MqBase: base,
		name:   "nsq_handler",
	}
}

// test hello
func (hl *mqHandler) helloWorld(ctx context.Context, msg *nsq.Message) error {
	req := &pb.HelloReq{}
	if errL := hl.ParseBody(msg.Body, req); errL != nil {
		return errL
	}
	if req.Id == -1 {
		panic("panic test3")
	}
	hl.logger.For(ctx).Info(fmt.Sprintf("Hello %s", req.Name))
	return nil
}

// Register 注册
func (hl *mqHandler) Register(c *mq.NsqConsumer) {
	topics := hl.cfg.Topics
	if err := c.Register("bmember_hello_world", "test", hl.helloWorld, 1); err != nil {
		hl.logger.Bg().Panic("nsq topic: bmember_hello_world", zap.Error(err))
	}
	if err := c.Register(topics.TopicOrderPayFinish, "bmember", hl.wrap(topics.TopicOrderPayFinish, hl.payOrderNotify), 5); err != nil {
		hl.logger.Bg().Panic(fmt.Sprintf("nsq topic: %s", topics.TopicOrderPayFinish), zap.Error(err))
	}
	if err := c.Register(topics.TopicOrderRefundFinish, "bmember", hl.wrap(topics.TopicOrderRefundFinish, hl.refundOrderNotify), 5); err != nil {
		hl.logger.Bg().Panic(fmt.Sprintf("nsq topic: %s", topics.TopicOrderRefundFinish), zap.Error(err))
	}
	if err := c.Register(topics.TopicMemberRegister, "bmember", hl.wrap(topics.TopicMemberRegister, hl.memberRegisterNotify), 5); err != nil {
		hl.logger.Bg().Panic(fmt.Sprintf("nsq topic: %s", topics.TopicMemberRegister), zap.Error(err))
	}
	if err := c.Register(topics.TopicCardSend, "bmember", hl.wrap(topics.TopicCardSend, hl.cardSendingNotify), 5); err != nil {
		hl.logger.Bg().Panic(fmt.Sprintf("nsq topic: %s", topics.TopicCardSend), zap.Error(err))
	}
}

// 支付订单消息通知
func (hl *mqHandler) payOrderNotify(ctx context.Context, msg *nsq.Message) error {
	req := &pay_pb.PayOrderView{}
	if errL := hl.ParseBody(msg.Body, req); errL != nil {
		return errL
	}
	// 支付成功订单进入下一步处理
	if req.OrderNumber == "" && req.Status != pay_pb.PayOrderStatus_PAY_ORDER_STATUS_SUCCESS {
		return nil
	}
	return service.GetAssetSrv().PayOrderFinishNotify(ctx, req)
}

// 退款订单消息通知
func (hl *mqHandler) refundOrderNotify(ctx context.Context, msg *nsq.Message) error {
	req := &pay_pb.RefundOrderView{}
	if errL := hl.ParseBody(msg.Body, req); errL != nil {
		return errL
	}
	// 支付成功订单进入下一步处理
	if req.OrderNumber == "" && req.Status != pay_pb.PayOrderStatus_REFUND_ORDER_STATUS_SUCCESS {
		return nil
	}
	return service.GetAssetSrv().RefundOrderFinishNotify(ctx, req)
}

// 会员注册消息
func (hl *mqHandler) memberRegisterNotify(ctx context.Context, msg *nsq.Message) error {
	req := &pb.MemberRegisterNotify{}
	if errL := hl.ParseBody(msg.Body, req); errL != nil {
		return errL
	}
	return service.GetMemberSrv().MemberRegisterNotify(ctx, req.BrandId, req.MemberId)
}

// 发卡消息
func (hl *mqHandler) cardSendingNotify(ctx context.Context, msg *nsq.Message) error {
	req := &pb.CardSendingNotify{}
	if errL := hl.ParseBody(msg.Body, req); errL != nil {
		return errL
	}
	return service.GetMemberSrv().CardSendingNotify(ctx, req.BrandId, req.AccId)
}
